package krow.dev.gunpobus.control.database;

import krow.dev.gunpobus.Constants;
import krow.dev.gunpobus.Constants.Database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataOpenHelper extends SQLiteOpenHelper {

	public DataOpenHelper(Context context){
		super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase database) {

		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append("CREATE TABLE ").append(Database.Favorite.TABLE_NAME).append(" (");
		sqlBuilder.append(Database.Favorite.COLUMN_NUMBER).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
		sqlBuilder.append(Database.Favorite.COLUMN_TYPE).append(" INTEGER DEFAULT 0, ");
		sqlBuilder.append(Database.Favorite.COLUMN_NAME).append(" TEXT NOT NULL, ");
		sqlBuilder.append(Database.Favorite.COLUMN_ID).append(" TEXT NOT NULL, ");
		sqlBuilder.append(Database.Favorite.COLUMN_ROUTE_TYPE).append(" INTEGER DEFAULT 0, ");
		sqlBuilder.append(Database.Favorite.COLUMN_STATION_SERIAL).append(" TEXT);");
		
		Log.e("test", "table: " + sqlBuilder.toString());
		
		database.beginTransaction();
		try {
			database.execSQL(sqlBuilder.toString());
			database.setTransactionSuccessful();
		} catch (Exception e) {
			// TODO 테이블 생성시 오류 처리, 삭제 후 다시 생성
			Log.e("test", "database create : " + e.toString());
		} finally{
			database.endTransaction();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO 버전 업데이트, 테이블 구조가 바뀌었을 경우 처리
	}
	
}
